package me.tuyou.servcie.impl;

import me.tuyou.entity.User;
import me.tuyou.servcie.UserService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    private final Map<Integer,User> users=new HashMap<>(16);

    public UserServiceImpl() {
        this.users.put(1,new User("czk","男",15));
        this.users.put(1,new User("czj","男",16));
        this.users.put(1,new User("czh","男",17));
    }

    @Override
    public Mono<User> findUserById(Integer id) {
        Mono<User> userMono = Mono.justOrEmpty(this.users.get(id));
        return userMono;
    }

    @Override
    public Flux<User> findAllUser() {
        return Flux.fromIterable(this.users.values());
    }

    @Override
    public Mono<Void> saveUserInfo(Mono<User> user) {
        return user.doOnNext(person->{
            int id=users.size()+1;
            users.put(id,person);
        }).thenEmpty(Mono.empty());
    }
}
